import java.util.Arrays;
import java.util.Scanner;

    public class NGuessNumber {
        public static int a_num = 0;
        public static int[] numbers = new int[4];
        public static int[] guesslist = new int[4];


        public static void main(String[] args) {

            Scanner scanner = new Scanner(System.in);

            while (true) {
                System.out.println("游戏开始(限制次数为7次)");

                int time = 0;
                int S = 7;
                System.out.println(Arrays.toString(numbers));
                while (time < 7) {

                    getPlayerInput();
                    compareGuessAnswer();

                    if (isWin()) {
                        System.out.println("恭喜你猜对了！");
                        break;
                    } else {
                        time++;
                        S--;
                        if (time == 7) {
                            System.out.println("7次到了，正确答案为" + Arrays.toString(numbers));
                            break;
                        }
                        System.out.println("猜错了，再试一下，还剩" + S + "次");
                    }
                }

                System.out.println("在玩一次(y/n)");
                String choice = scanner.next();
                if (!choice.equalsIgnoreCase("y")) {
                    break;
                }
            }

        }
            //生成四个数字
            public int[] generateAnswer () {
                int a, b, c, d;
                int[] e = new int[]{};
                for (; ; ) {
                    a = (int) (Math.random() * 10);
                    b = (int) (Math.random() * 10);
                    c = (int) (Math.random() * 10);
                    d = (int) (Math.random() * 10);
                    if (a != b && a != c && a != d && b != c && b != d) {
                        e = new int[]{a, b, c, d};
                        break;
                    }
                }
                return e;
            }

            //接收玩家输入的4个数字并返回。
            public static int[] getPlayerInput () {

                try {
                    Scanner sca = new Scanner(System.in);

                    for (int i = 0; i < 4; i++) {
                        System.out.println("请输入0-9之间的第" + (i + 1) + "个数字：");

                        int l = sca.nextInt();
                        if (l >= 10 || l < 0) {
                            System.out.print("输入错误，");
                            i = i - 1;
                        } else {
                            guesslist[i] = l;
                        }
                    }
                } catch (Exception e) {
                    System.out.println("输入错误");
                }

                System.out.println(Arrays.toString(guesslist) + "\n");
                return guesslist;
            }

            public static String compareGuessAnswer () {
                a_num = 0;

                for (int i = 0; i < numbers.length; i++) {
                    for (int j = 0; j < numbers.length; j++) {
                        if (i != j) {
                        } else {
                            if (numbers[i] == guesslist[j]) {
                                a_num++;
                            }
                        }
                    }
                }
                System.out.println(a_num + "A");
                System.out.println("A表示存在且位置正确的数字个数");
                System.out.println("如：1A2B,表示你猜测的四个数字有3个是对的，有一个位置对，两个位置不对。\n");
                return a_num + "A" + "A表示存在且位置正确的数字个数\n";
            }
            public static boolean isWin () {
                boolean flag = false;
                //根据提示信息中A的数量判断玩家是否获胜，如果获胜返回true，否则返回false。
                if (a_num == 4) {
                    flag = true;
                } else {
                    flag = false;
                }
                return flag;
            }


    }